package com.corpgovernment.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

@Slf4j
@Deprecated
public class EncryptUtils {
    private EncryptUtils() {
    }

    private static String KEY = "1234567890123456";
    private static SymmetricCrypto SM4 = SmUtil.sm4();

    /**
     * DES 加密
     *
     * @param key 长度必须大于等于8
     */
    public static byte[] desEncrypt(byte[] content, byte[] key) throws Exception {
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(key);
            SecretKeyFactory keyFty = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFty.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
            return cipher.doFinal(content);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * DES 解密
     *
     * @param key 长度必须大于等于8
     */
    public static byte[] desDecrypt(byte[] content, byte[] key) throws Exception {
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
            return cipher.doFinal(content);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * sm4加密（加密失败返回原文）
     *
     * @param content
     * @return
     */
    public static String sm4Encrypt(String content) {
        if (isEncryption(content)) {
            return content;
        }
        SymmetricCrypto sm4 = SmUtil.sm4(KEY.getBytes());
        try {
            return sm4.encryptBase64(content);
        } catch (Exception ex) {
            log.error("加密失败,content:" + content, ex);
        }
        return content;
    }

    /**
     * 判断content是否为加密字符
     *
     * @param content
     * @return
     */
    public static boolean isEncryption(String content) {
        SymmetricCrypto sm4 = SmUtil.sm4(KEY.getBytes());
        try {
            sm4.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * sm4解密（解密失败返回密文）
     *
     * @param content
     * @return
     */
    public static String sm4Decrypt(String content) {
        if (StringUtils.isBlank(content)) {
            return content;
        }
        SymmetricCrypto sm4 = SmUtil.sm4(KEY.getBytes());
        try {
            return sm4.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
        } catch (Exception ex) {
            log.info("解密失败,content:" + content);
        }
        return content;
    }
}
